//
//  JGSettingCell.m
//  WangYLottery
//
//  Created by JJetGu on 15/8/23.
//  Copyright (c) 2015年 JJetGu. All rights reserved.
//

#import "JGSettingCell.h"

#import "JGSettingItem.h"

#import "JGSettingArrowItem.h"
#import "JGSettingSwitchItem.h"
#import "JGSettingLabelItem.h"

@interface JGSettingCell ()

@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UISwitch *switchView;
@property (nonatomic, strong) UILabel *labelView;

@property (nonatomic, weak) UIView *divider;

@end

@implementation JGSettingCell

-(UIView *)divider
{
    if (!_divider) {
        if (!ios7) {
            UIView *divider  = [[UIView alloc] init];
            divider.backgroundColor = [UIColor blackColor];
            divider.alpha = 0.2;
            
            _divider = divider;
        }
    }
    
    return _divider;
}

-(UIImageView *)imgView
{
    if (_imgView == nil) {
        UIImage *image = [UIImage imageNamed:@"CellArrow"];
        self.imgView = [[UIImageView alloc] initWithImage:image];
    }
    
    return _imgView;
}

-(UISwitch *)switchView
{
    if (_switchView == nil) {
        self.switchView = [[UISwitch alloc] init];
    }
    
    return _switchView;
}

-(UILabel *)labelView
{
    if (_labelView == nil) {
        self.labelView = [[UILabel alloc] init];
        self.labelView.bounds = CGRectMake(0, 0, 100, 44);
        self.labelView.textColor = [UIColor redColor];
        self.labelView.textAlignment = NSTextAlignmentRight;
    }
    
  return _labelView;
}

-(void)setItem:(JGSettingItem *)item
{
    _item = item;
    
    // 1.设置cell的子控件的数据
    [self setUpData];
    
    // 2.设置右边视图
    [self setUpAccessoryView];
    
}

// 设置cell的子控件的数据
- (void)setUpData
{
    if (_item.icon.length) {
        UIImage *image = [UIImage imageNamed:_item.icon];
        self.imageView.image = image;
    }
    
    self.textLabel.text = _item.title;
    self.detailTextLabel.text = _item.subTitle;
}

// 设置右边视图
- (void)setUpAccessoryView
{
    if ([_item isKindOfClass:[JGSettingArrowItem class]]) { // 箭头
        self.accessoryView = self.imgView;
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
    }else if ([_item isKindOfClass:[JGSettingSwitchItem class]]){ // Switch
        self.accessoryView = self.switchView;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }else if ([_item isKindOfClass:[JGSettingLabelItem class]]){
        self.accessoryView = self.labelView;
        
        JGSettingLabelItem *labelItem = (JGSettingLabelItem *)_item;
        self.labelView.text = labelItem.text;
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
    }else{
        self.accessoryView = nil;
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
    }
}

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"cell";
    JGSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID ];
    
    if (cell == nil) {
        cell = [[JGSettingCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    
    return cell;
}

#pragma mark - 判断分割线是否需要显示
-(void)setIndexPath:(NSIndexPath *)indexPath
{
    _indexPath = indexPath;
    
    self.divider.hidden = indexPath.row == 0;
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        // 设置背景
        [self setUpBg];
        // 清空子视图的背景
        [self setSubViews];
    }
    
    
    return self;
}

- (void)setUpBg
{
    // 设置背景图片
    UIView *bg = [[UIView alloc] init];
    bg.backgroundColor = [UIColor whiteColor];
    self.backgroundView = bg;
    
    // 设置选中的背景图片
    UIView *selectedBg = [[UIView alloc] init];
    selectedBg.backgroundColor = JGColor(237, 233, 218);
    self.selectedBackgroundView = selectedBg;
}


- (void)setSubViews
{
    self.textLabel.backgroundColor = [UIColor clearColor];
    self.detailTextLabel.backgroundColor = [UIColor clearColor];
}


-(void)setFrame:(CGRect)frame
{
    if (!ios7) {
        frame.size.width += 20;
        frame.origin.x -= 10;
    }
    [super setFrame:frame];
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat dividerX = self.textLabel.frame.origin.x;
    CGFloat dividerY = 0;
    CGFloat dividerH = 1;
    CGFloat dividerW = 320;
    
    self.divider.frame = CGRectMake(dividerX, dividerY, dividerW, dividerH);
    
}

@end
